package dao.html;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.*;
import java.net.URLConnection;
import java.io.IOException;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Denis
 * Date: 13.06.13
 * Time: 23:05
 * To change this template use File | Settings | File Templates.
 */
public class HtmlParser {
    private BufferedReader incoming;
    private String data,HOST;
    private int point;
    public HtmlParser(String HOST) throws IOException{
        this.HOST=HOST;
    }
    public Set<String> getLinks() throws IOException{
        List<String> links = new ArrayList<String>();
        Set<String> links1 = new HashSet<String>();
        URL url= new URL(HOST);
        URLConnection urlcon= url.openConnection();
        incoming= new BufferedReader(new InputStreamReader(urlcon.getInputStream()));
        while((data=incoming.readLine())!=null){
            while((point=data.indexOf("href=\""))!=-1){
                 data=data.substring(point);
                 links.add(data);
                 data=data.substring(6);
                }
            }
            for(int i=0;i<links.size();i++){
                links.set(i,links.get(i).substring(6));
                if(((links.get(i).startsWith(data="./"))||(links.get(i).startsWith(data="../"))||((links.get(i).startsWith(data="/"))))){
                    links.set(i, HOST+'/'+links.get(i).substring(data.length()));
                } else if(links.get(i).startsWith("asn")) links.set(i, HOST+'/'+links.get(i));
                if(((point=links.get(i).indexOf('\"'))!=-1)||(((point=links.get(i).indexOf('\''))!=-1))){
                    if((links.get(i).startsWith("http:"))||(links.get(i).startsWith("https:"))) links.set(i, links.get(i).substring(0, point));
                    else links.set(i, "http:"+links.get(i).substring(0, point));}
                if((links.get(i).endsWith("//"))||((links.get(i).endsWith("#")))) links.set(i,HOST);
                }
        for(String x: links){
            links1.add(x);
        }
        return links1;
    }

}
